package com.orgcent.tuangou.util;

import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import static com.orgcent.tuangou.util.Constants.*;

import android.util.Log;

/**
 * 基本工具类
 */
public class CommonUtil {
	/**
	 * MD5加密
	 * @param text
	 * @return
	 */
	public final static String md5(String text) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		String encodingStr = null;
		try {
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(text.getBytes("utf-8"));
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				str[k++] = hexDigits[md[i] >>> 4 & 0xf];
				str[k++] = hexDigits[md[i] & 0xf];
			}
			encodingStr = new String(str);
		} catch (Exception e) {
		}
		return encodingStr;
	}
	
	public static String trim(String text) {
		return text == null ? "" : text;
	}
	
	//格式化秒为小时、分、秒
	public static String formatSecond(long seconds) {
		int d = (int)(seconds / (24 * 3600));
		seconds = seconds % (24 * 3600);
		int h = (int)(seconds / 3600);
		seconds = seconds % 3600;
		int m = (int)(seconds / 60);
		seconds = seconds % 3600;
		int s = (int)(seconds % 60);
		return d + "天" + (h<10?"0"+h:h) + "时" + (m<10?"0"+m:m) + "分" + (s<10?"0"+s:s) + "秒";
	}
	
	//计算节省了多少
	public static String toSavingAmount(String srcPrice,String nowPrice) {
		try {
			float src = Float.parseFloat(srcPrice);
			float now = Float.parseFloat(nowPrice);
			
			return String.valueOf(src - now);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	public final static String date2str(Date d) {
		return new SimpleDateFormat("MM月dd日").format(d);
	}
	
	public static String formatNum(float f) {
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(f);
	}
	
	/**
	 * 人性化格式的日期
	 * @param unix_time
	 * @return
	 */
	public static String toGEForamt(String unix_time) {
		String time = "未知";
		try {
			long endTime = Long.parseLong(unix_time) * 1000;
			long now = System.currentTimeMillis();
			long duration = endTime - now;
			if(duration < ONE_MINUTE) {
				time = "马上过期";
			} else if(duration < ONE_HOUR) {
				time = formatNum(duration * 1F / ONE_MINUTE) + "分钟后过期";
			} else if(duration < ONE_DAY) {
				time = formatNum(duration * 1F / ONE_HOUR) + "小时后过期";
			} else if(duration < ONE_DAY * 2 
					&& duration >= ONE_DAY) {
				time = "明天过期";
			} else if(duration < ONE_DAY * 3 
				&& duration >= ONE_DAY * 2) {
				time = "后天过期";
			} else if(duration >= ONE_DAY * 3) {
				time = CommonUtil.date2str(new java.util.Date(endTime)) + "过期";
			} else if(duration <= 0) {
				time = "已过期";
			}
			Log.i("CommonUtil", "duration=" + duration + ",time=" + time );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}
}
